package com.example.hosrxjavaapplication.observable;

import ohos.aafwk.ability.AbilitySlice;

import java.util.LinkedHashMap;

/**
 * 接收者
 * create by chenjiayou on 2021/2/19
 */
public class Observables {

    private static LinkedHashMap<String, Observable> observableLinkedHashMap;

    private static void init() {
        if (observableLinkedHashMap == null) {
            observableLinkedHashMap = new LinkedHashMap<>();
        }
    }

    public static ObservableImpl createObservable(AbilitySlice context, ObservableOnSubscribe subscribe) {
        init();
        String tag = String.valueOf(System.currentTimeMillis());
        ObservableImpl observable = new ObservableImpl(context, tag, subscribe);
        observableLinkedHashMap.put(tag, observable);
        return observable;
    }

    public static Observable createWorkObservable(AbilitySlice context) {
        init();
        String tag = String.valueOf(System.currentTimeMillis());
        ObservableWorkImpl observable = new ObservableWorkImpl(context, tag);
        observableLinkedHashMap.put(tag, observable);
        return observable;
    }

    public static void clear() {
        if (observableLinkedHashMap != null) {
            observableLinkedHashMap.clear();
            observableLinkedHashMap = null;
        }
    }
}
